package com.ton.config;

import com.kypj.frame.db.MyTemplate;
import lombok.Data;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;


@Configuration
@Data
public class MasterDataSourceConfiguration {
    @Value(value = "${master.datasource.tomcat.driver-class-name}")
    private String driverClassName;

    @Value(value = "${master.datasource.tomcat.url}")
    private String url;

    @Value(value = "${master.datasource.tomcat.username}")
    private String username;

    @Value(value = "${master.datasource.tomcat.password}")
    private String password;

    @Value(value = "${master.datasource.tomcat.max-active}")
    private int maxActive;

    @Value(value = "${master.datasource.tomcat.max-idle}")
    private int maxIdle;

    @Value(value = "${master.datasource.tomcat.min-idle}")
    private int minIdle;

    @Value(value = "${master.datasource.tomcat.initial-size}")
    private int initialSize;

    @Value(value = "${master.datasource.tomcat.test-on-borrow}")
    private boolean testOnBorrow;

    @Value(value = "${master.datasource.tomcat.test-on-return}")
    private boolean testOnReturn;

    @Value(value = "${master.datasource.tomcat.test-while-idle}")
    private boolean testWhileIdle;

    @Value(value = "${master.datasource.tomcat.time-between-eviction-runs-millis}")
    private int timeBetweenEvictionRunsMillis;

    @Value(value = "${master.datasource.tomcat.min-evictable-idle-time-millis}")
    private int minEvictableIdleTimeMillis;

    @Value(value = "${master.datasource.tomcat.max-wait}")
    private int maxWait;


    @Bean("masterDataSource")
    public DataSource dataSourcePrimary() {
        org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setMaxActive(maxActive);
        dataSource.setMaxIdle(maxIdle);
        dataSource.setMinIdle(minIdle);
        dataSource.setInitialSize(initialSize);
        dataSource.setTestOnBorrow(testOnBorrow);
        dataSource.setTestOnReturn(testOnReturn);
        dataSource.setTestWhileIdle(testWhileIdle);
        dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        dataSource.setMaxWait(maxWait);
        System.out.println("------->>>>>>>>URL:"+url);
        return dataSource;
    }

    @Bean("masterTransactionManager")
    @Primary
    public PlatformTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource masterDataSource) {
        return new DataSourceTransactionManager(masterDataSource);
    }

    @Bean(name = "masterJdbcTemplate")
    @Primary
    public JdbcTemplate masterJdbcTemplate(
            @Qualifier("masterDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "masterMyTemplate")
    @Primary
    public MyTemplate masterMyTemplate(@Qualifier("masterJdbcTemplate") JdbcTemplate masterJdbcTemplate) {
        MyTemplate my = new MyTemplate();
        my.setJdbcTemplate(masterJdbcTemplate);
        return my;
    }
}
